package cn.com.wangluotongxin.linux.utils;

import cn.com.wangluotongxin.common.constants.CommonConstant;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SortUtil {


    public static <T>List<T> orderBy(List<Map> maps, String orderField, String orderBy, Class dataTypeClass, Class classes) {
        List<String> dataTypeClasses = Arrays.asList(dataTypeClass.getName().split("\\."));
        List<Map> orderByDataMaps = maps;
        int dataSize = maps.size();
        for (int i = 0; i < dataSize; i++) {
            for (int j = 0; j < dataSize - 1 - i; j++) {
                Map beforeMap = maps.get(j);
                Map afterMap = maps.get(j+1);

                // 日期 date
                if(dataTypeClasses.get(dataTypeClasses.size() - 1).equals("Date")){
                    String beforeDate = MapUtil.getStr(beforeMap, orderField);
                    String afterDate = MapUtil.getStr(afterMap, orderField);
                    if(StrUtil.isBlank(beforeDate) || StrUtil.isBlank(afterDate))continue;
                    beforeDate = DateSysUtils.isDate(beforeDate.concat(" 00:00:00")) ? beforeDate.concat(" 00:00:00") : beforeDate.concat("-01 00:00:00");
                    afterDate = DateSysUtils.isDate(afterDate.concat(" 00:00:00")) ? afterDate.concat(" 00:00:00") : afterDate.concat("-01 00:00:00");
                    if(orderBy.equals("desc")){
                        if (DateUtil.parseDateTime(beforeDate).compareTo(DateUtil.parseDateTime(afterDate)) <= 0) {
                            orderByDataMaps.set(j, afterMap);
                            orderByDataMaps.set(j + 1, beforeMap);
                        }
                    }else {
                        if (DateUtil.parseDateTime(afterDate).compareTo(DateUtil.parseDateTime(beforeDate)) < 0) {
                            orderByDataMaps.set(j, afterMap);
                            orderByDataMaps.set(j + 1, beforeMap);
                        }
                    }
                }

                // 数量 integer
                if(dataTypeClasses.get(dataTypeClasses.size() - 1).equals("Integer")){
                    Integer beforeDate = MapUtil.getInt(beforeMap, orderField, CommonConstant.ZERO);
                    Integer afterDate = MapUtil.getInt(afterMap, orderField, CommonConstant.ZERO);
                    if(orderBy.equals("desc")){
                        if (beforeDate <= afterDate) {
                            orderByDataMaps.set(j, afterMap);
                            orderByDataMaps.set(j + 1, beforeMap);
                        }
                    }else {
                        if (afterDate < beforeDate) {
                            orderByDataMaps.set(j, afterMap);
                            orderByDataMaps.set(j + 1, beforeMap);
                        }
                    }
                }

                // 金额 BigDecimal
                if(dataTypeClasses.get(dataTypeClasses.size() - 1).equals("BigDecimal")){
                    BigDecimal beforeDate = BigDecimal.valueOf(MapUtil.getDouble(beforeMap, orderField, 0.0));
                    BigDecimal afterDate = BigDecimal.valueOf(MapUtil.getDouble(afterMap, orderField, 0.0));
                    if(orderBy.equals("desc")){
                        if (beforeDate.compareTo(afterDate) <= 0) {
                            orderByDataMaps.set(j, afterMap);
                            orderByDataMaps.set(j + 1, beforeMap);
                        }
                    }else {
                        if (afterDate.compareTo(beforeDate) < 0) {
                            orderByDataMaps.set(j, afterMap);
                            orderByDataMaps.set(j + 1, beforeMap);
                        }
                    }
                }
            }
        }
        return JSONObject.parseArray(JSONObject.toJSONString(orderByDataMaps), classes);
    }
}
